Imports BlazeApps.Library.Polls

Partial Class Usercontrols_Polls_AddPoll
    Inherits UserControlBase

#Region " Enums, Memebers and Properties "

    Protected Property PollGuid() As Guid
        Get
            Return GetGuidFromVS("PollID")
        End Get
        Set(ByVal value As Guid)
            ViewState.Add("PollID", value)
        End Set
    End Property

    Protected Property NewButtonClicked() As Boolean
        Get
            Return GetBooleanFromVS("NewButtonClicked")
        End Get
        Set(ByVal value As Boolean)
            ViewState.Add("NewButtonClicked", value)
        End Set
    End Property

#End Region

#Region " Page Events "

    Protected Sub Page_Init(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Init
        Me.MessageLabel = Me.uxMessagesLabel
    End Sub

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

        SetPageTitle("Create Poll")

        If IsUserAuthenticated And (EnableUserPolls OrElse IsPollModerator()) Then

            If (PollGuid = Guid.Empty) Then
                GetQueryStringValues()
            End If

            If Me.Page.IsPostBack = False Then
                If Not (PollGuid = Guid.Empty) Then
                    LoadPoll()
                    LoadAnswers()
                End If
            End If

            LoadSettings()

            Me.uxMemberPollPanel.Visible = ShowControls(SecurityMask.Member, True)

        Else
            Me.uxMemberPollPanel.Visible = False
            Me.uxMessagesLabel.Text = Resources.Resource.AdminDisabledPolls.ToString
        End If

    End Sub

#End Region

#Region " Save Data "

    Private Sub SavePollQuestion()
        Dim Item As PollQuestionDetail = PollQuestionDetail.NewPollQuestionDetail

        Item.PollQuestion = Me.uxPollBodyTextbox.Text.ToString.Trim
        Item.ExpirationDateString = Me.uxExpirationDateTextBox.Text.ToString.Trim
        Item.UserID = CurrentUserID

        Item.Save()

        PollGuid = Item.PollId

        uxOptions.ItemGuid = Item.PollId
        uxOptions.SetInitialOptions = True
        uxOptions.SaveOptions()
        uxOptions.LoadOptions()

        LoadSettings()

    End Sub

    Private Sub UpdatePollQuestion()
        Dim Item As PollQuestionDetail = PollQuestionDetail.GetPollQuestionDetail(PollGuid)

        Item.PollId = PollGuid
        Item.PollQuestion = Me.uxPollBodyTextbox.Text.ToString.Trim
        Item.ExpirationDateString = Me.uxExpirationDateTextBox.Text.ToString.Trim

        Item.Save()

    End Sub

    Private Sub SavePollAnswers()
        Dim Item As PollQuestionDetail = PollQuestionDetail.NewPollQuestionDetail
    End Sub

#End Region

#Region " Load Data "

    Private Sub LoadSettings()
        If (PollGuid = Guid.Empty) Then
            Me.uxOptionsTabPanels.Enabled = False
            Me.uxMyGalleryTabPanel.Enabled = False
            Me.uxAttachmentsTabPanel.Enabled = False
            Me.uxAddAnswersPanel.Visible = False
            Me.uxAddAnswersPanel.Enabled = False
        Else
            Me.uxOptionsTabPanels.Enabled = True
            uxMyGalleryTabPanel.Enabled = (EnableUserGalleries OrElse IsAdmin())
            uxAttachmentsTabPanel.Enabled = (EnableUserGalleries OrElse IsAdmin())
            Me.uxAddAnswersPanel.Visible = True
            Me.uxAddAnswersPanel.Enabled = True
        End If

        Me.uxMyGallery.IsLoadedAsOption = True

    End Sub

    Private Sub LoadPoll()

        Dim Item As PollQuestionDetail = PollQuestionDetail.GetPollQuestionDetail(PollGuid)

        Me.uxPollBodyTextbox.Text = Item.PollQuestion
        Me.uxExpirationDateTextBox.Text = Item.ExpirationDateString

    End Sub

    Private Sub LoadAnswers()
        Dim List As PollAnswerParent = PollAnswerParent.GetPollAnswerParent(PollGuid)

        With Me.uxPollAnswerRepeater
            .DataSource = List
            .DataBind()
        End With

    End Sub

#End Region

#Region " Form Controls Events "

    Protected Sub uxAddAnswerButton_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles uxAddAnswerButton.Click

        Dim Item As PollAnswerDetail = PollAnswerDetail.NewPollAnswerDetail()

        Item.PollId = PollGuid
        Item.AnswerText = Me.uxPollAnswerTextBox.Text.ToString.Trim

        Item.Save()

        LoadAnswers()

    End Sub

    Protected Sub uxNewPollButton_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles uxNewPollButton.Click
        'Response.Redirect(String.Format("{0}.aspx?pid=pol_addpoll", AppPage))

        NewButtonClicked = True

        PollGuid = Guid.Empty
        LoadSettings()

        Me.uxPollBodyTextbox.Text = String.Empty
        Me.uxExpirationDateTextBox.Text = String.Empty
    End Sub

    Protected Sub uxSubmitPollButton_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles uxSubmitPollButton.Click
        If PollGuid = Guid.Empty Then
            SavePollQuestion()
        Else
            UpdatePollQuestion()
        End If
    End Sub

    Protected Sub uxDeletePollButton_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles uxDeletePollButton.Click
        PollQuestionDetail.DeletePollQuestionDetail(PollGuid)

        Response.Redirect(String.Format("{0}.aspx?pid=pol_Polllist", AppPage))
    End Sub

    Protected Sub uxResetPollButton_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles uxResetPollButton.Click
        If Not (PollGuid = Guid.Empty) Then
            LoadPoll()
        Else
            Me.uxPollBodyTextbox.Text = String.Empty
            Me.uxExpirationDateTextBox.Text = String.Empty
        End If

        LoadSettings()
    End Sub

    Protected Sub DeleteAnswers(ByVal sender As Object, ByVal e As CommandEventArgs)

        PollAnswerDetail.DeletePollAnswerDetail(PollGuid, ConvertToGuid(e.CommandArgument.ToString()))

        LoadAnswers()

    End Sub

#End Region

#Region " Helpers and Methods "

    Protected Sub GetQueryStringValues()
        If NewButtonClicked = False Then
            PollGuid = GetGuidFromQueryString("id")
        End If
    End Sub

#End Region

End Class
